package com.hit.sys.modular.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.core.enums.CommonStatusEnum;
import com.hit.core.exception.ServiceException;
import com.hit.core.util.CryptogramUtil;
import com.hit.sys.modular.user.entity.SysUser;
import com.hit.sys.modular.user.enums.SysUserExceptionEnum;
import com.hit.sys.modular.user.factory.SysUserFactory;
import com.hit.sys.modular.user.mapper.SysUserMapper;
import com.hit.sys.modular.user.param.SysUserParam;
import com.hit.sys.modular.user.result.SysUserResult;
import com.hit.sys.modular.user.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Override
    public SysUserResult detail(SysUserParam sysUserParam) {
        SysUserResult sysUserResult = new SysUserResult();

        SysUser sysUser = this.getEntity(sysUserParam);
        BeanUtil.copyProperties(sysUser, sysUserResult);

        return sysUserResult;
    }

    @Override
    public void add(SysUserParam sysUserParam) {
        checkParam(sysUserParam, false);
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(sysUserParam, sysUser);
        SysUserFactory.fillAddCommonUserInfo(sysUser);
        if(ObjectUtil.isNotEmpty(sysUserParam.getPassword())) {
            sysUser.setPassword(CryptogramUtil.doHashValue(sysUserParam.getPassword()));
        }

        this.save(sysUser);
    }

    @Override
    public void edit(SysUserParam sysUserParam) {
        checkParam(sysUserParam, true);
        SysUser sysUser = this.getEntity(sysUserParam);
        BeanUtil.copyProperties(sysUserParam, sysUser);
        if(ObjectUtil.isNotEmpty(sysUserParam.getPassword())){
            sysUser.setPassword(CryptogramUtil.doHashValue(sysUserParam.getPassword()));
        }
        sysUser.setStatus(null);
        this.updateById(sysUser);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SysUserParam> sysUserParamList) {
        sysUserParamList.forEach(sysUserParam -> {
            SysUser sysUser = this.getEntity(sysUserParam);
            sysUser.setStatus(CommonStatusEnum.DELETED.getCode());
            this.updateById(sysUser);
        });
    }

    @Override
    public SysUser getByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount, account);
        queryWrapper.ne(SysUser::getStatus, CommonStatusEnum.DELETED.getCode());

        return this.getOne(queryWrapper);
    }

    private void checkParam(SysUserParam sysUserParam, boolean isExcludeSelf) {
        Long id = sysUserParam.getId();
        String account = sysUserParam.getAccount();

        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount, account)
                .ne(SysUser::getStatus, CommonStatusEnum.DELETED.getCode());

        //是否排除自己，如果是则查询条件排除自己id
        if (isExcludeSelf) {
            queryWrapper.ne(SysUser::getId, id);
        }

        int countByAccount = this.count(queryWrapper);
        //大于等于1个则表示重复
        if (countByAccount >= 1) {
            throw new ServiceException(SysUserExceptionEnum.USER_ACCOUNT_REPEAT);
        }
    }

    private SysUser getEntity(SysUserParam sysUserParam){
        SysUser entity = this.getById(sysUserParam.getId());
        if(ObjectUtil.isNull(entity)){
            throw new ServiceException(SysUserExceptionEnum.USER_NOT_EXIST);
        }
        return entity;
    }
}
